package com.crawler.gmail;

import com.alibaba.fastjson.JSON;
import com.crawler.gmail.crawler.FetchAllEmail;
import com.crawler.gmail.model.EmailFetchSettingParam;
import com.crawler.gmail.model.RestJson;
import com.crawler.gmail.utils.JsonUtil;
import org.apache.http.HttpEntity;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.protocol.BasicHttpContext;
import org.apache.http.util.EntityUtils;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler;

import java.io.IOException;

@SpringBootApplication
public class App {

    public static void main(String[] args) {
        SpringApplication.run(App.class, args);
        ThreadPoolTaskScheduler jobListTaskScheduler = new ThreadPoolTaskScheduler();
        jobListTaskScheduler.initialize();
        jobListTaskScheduler.scheduleWithFixedDelay(new FetchAllEmail(), 1000 * 60 * 5);



//        EmailFetchSettingParam email = new EmailFetchSettingParam();
//        email.setEmail("waq918@gmail.com");
//        email.setPassword("asdasd");
//        email.setPort(993);
//        email.setHostname("imap.gmail.com");
//        email.setSsl(1);
//        email.setProtocol(1);
//        httpPost("http://8.218.209.104:8080/verify-gmail-info", email);
    }

    public static String httpPost(String url, Object data) {
        HttpPost httpPost = new HttpPost(url);
        CloseableHttpResponse response = null;
        CloseableHttpClient httpClient = HttpClients.createDefault();
        RequestConfig requestConfig = RequestConfig.custom().
                setSocketTimeout(2000).setConnectTimeout(2000).build();
        httpPost.setConfig(requestConfig);
        httpPost.addHeader("Content-Type", "application/json");

        try {
            StringEntity requestEntity = new StringEntity(JSON.toJSONString(data), "utf-8");
            httpPost.setEntity(requestEntity);

            response = httpClient.execute(httpPost, new BasicHttpContext());

            if (response.getStatusLine().getStatusCode() != 200) {

//                LOGGER.error("request url failed, http code=" + response.getStatusLine().getStatusCode()
//                        + ", url=" + url);
                return null;
            }
            HttpEntity entity = response.getEntity();
            if (entity != null) {
                String resultStr = EntityUtils.toString(entity, "utf-8");
//                LOGGER.info("httpPost resultStr:" + resultStr);
                return resultStr;
            }
        } catch (Exception e) {
//            LOGGER.error(e.getMessage(), e);
        } finally {
            if (response != null) {
                try {
                    response.close();
                } catch (IOException e) {
//                    LOGGER.error(e.getMessage(), e);
                }
            }
        }

        return null;
    }

}
